package com.example.myapplication10.Activities;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication10.Buttons.Btn_Add_Inner;
import com.example.myapplication10.Buttons.Btn_Del_Inner;
import com.example.myapplication10.MsgEvents.MsgEventAddCity;
import com.example.myapplication10.MsgEvents.MsgEventException;
import com.example.myapplication10.MsgEvents.MsgEventRemoveCity;
import com.example.myapplication10.MsgEvents.MsgEventRemoveIntent;
import com.example.myapplication10.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.VH> {
    private boolean delFlag = false;
    private boolean prevDelFlag = false;

    public boolean isDelFlag() {
        return delFlag;
    }

    public void setDelFlag(boolean delFlag) {
        this.delFlag = delFlag;
    }

    private List<Map<String,String>> datas;
    private List<Map<String,String>> allDatas = new ArrayList<>();
    private List<Map<String,String>> usedDatas;
    public int sizeInner = 0;
    public int sizeOuter = 0;
    public List<Map<String, String>> getDatas() {
        return datas;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<Map<String, String>> datas, int sizeOuter) {
        this.datas = datas;
        this.sizeOuter = sizeOuter;
    }

    public RecycleAdapter(){
        EventBus.getDefault().register(this);
        this.datas = new ArrayList<>();
        this.usedDatas = new ArrayList<>();
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.add_data,parent,false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, @SuppressLint("RecyclerView") int position) {
        String txt = String.valueOf(position+1);
        holder.index.setText(txt);
        txt = (datas.get(position).get("城市名称"));
        holder.city0.setText(txt);
        txt = (datas.get(position).get("城市代码"));
        holder.code0.setText(txt);
        if (isDelFlag() && sizeInner < sizeOuter -1){
            prevDelFlag = true;
            holder.btn_add0.setVisibility(View.GONE);
            holder.btn_del0.setVisibility(View.VISIBLE);
            holder.btn_del0.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("NotifyDataSetChanged")
                @Override
                public void onClick(View v) {
                    usedDatas.add(datas.get(position));
                    if (allDatas.contains(datas.get(position))){
                        EventBus.getDefault().post(new MsgEventException("不能同时添加和删除同一城市！"));
                        try {
                            RecycleAdapter.this.finalize();
                        } catch (Throwable e) {
                            throw new RuntimeException(e);
                        }
                    }
                    else{
                        EventBus.getDefault().post(new MsgEventRemoveCity(datas.get(position)));
                        EventBus.getDefault().post(new MsgEventRemoveIntent(datas.get(position)));
                    }
                    allDatas.addAll(usedDatas);
                    usedDatas.clear();
                    sizeInner = allDatas.size();
                    datas.remove(position);
                    RecycleAdapter.this.notifyDataSetChanged();
                }
            });
        }
        else {
            prevDelFlag = false;
            holder.btn_add0.setVisibility(View.VISIBLE);
            holder.btn_del0.setVisibility(View.GONE);
            holder.btn_add0.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("NotifyDataSetChanged")
                @Override
                public void onClick(View v) {
                    usedDatas.add(datas.get(position));
                    if (allDatas.contains(datas.get(position))){
                        EventBus.getDefault().post(new MsgEventException("不能同时添加和删除同一城市！\n点击确定退出程序"));
                        try {
                            RecycleAdapter.this.finalize();
                        } catch (Throwable e) {
                            throw new RuntimeException(e);
                        }
                    }
                    else if (sizeOuter > 15){
                        EventBus.getDefault().post(new MsgEventException("最多可同时添加15个城市！"));
                        try {
                            RecycleAdapter.this.finalize();
                        } catch (Throwable e) {
                            throw new RuntimeException(e);
                        }
                    }

                    else {
                        EventBus.getDefault().post(new MsgEventAddCity(datas.get(position)));
                    }
                    allDatas.addAll(usedDatas);
                    usedDatas.clear();
                    sizeInner = allDatas.size();
                    datas.remove(position);
                    RecycleAdapter.this.notifyDataSetChanged();
                }
            });
        }

    }

    //        else {
//            holder.btn_add0.setVisibility(View.INVISIBLE);
//            holder.btn_del0.setVisibility(View.INVISIBLE);
//        }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageEventException(MsgEventException event) {
//        EventBus.getDefault().post(new MsgEventTitle(isOver));
    }

    public static class VH extends RecyclerView.ViewHolder{
        TextView index,city0,code0;
        Btn_Add_Inner btn_add0;
        Btn_Del_Inner btn_del0;
        public VH(@NonNull View itemView) {
            super(itemView);
            index = itemView.findViewById(R.id.index0);
            city0 = itemView.findViewById(R.id.city0);
            code0 = itemView.findViewById(R.id.code0);
            btn_add0 = itemView.findViewById(R.id.btn_add0);
            btn_del0 = itemView.findViewById(R.id.btn_del0);
        }
    }
}


